package com.wanhe.viewconf.views.base.text;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

/**
 * File Name:AutoTextSizeTextView.java<br/>
 * Package Name:com.wanhe.viewconf.views.base.text<br/>
 * Date:2017/7/25 15:13<br/>
 * author:chendeji<br/>
 * desc: 能够自动调整本身的字体大小的文本控件<br/>
 * Copyright (c) 2017/7/25, 781571323@qq.com All Rights Reserved.
 *
 */
public class AutoTextSizeTextView extends TextView {
	public AutoTextSizeTextView(Context context) {
		super(context);
	}

	public AutoTextSizeTextView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		float parentHeight = MeasureSpec.getSize(heightMeasureSpec);
		float height = Math.max(getMeasuredHeight(), getMeasuredWidth());
		float textSize = getTextSize();
		float scale = parentHeight / height;
		float newTextSize = textSize * scale;
		setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSize - 5);
		float textWidth = getPaint().measureText(getText().toString());
		super.onMeasure(MeasureSpec.makeMeasureSpec((int) textWidth, MeasureSpec.EXACTLY), heightMeasureSpec);

	}
}
